/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 1/13/2009
 * Time: 8:39 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Data;

namespace QuickMail
{
	/// <summary>
	/// Description of Mail.
	/// </summary>
	public class Mail : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox txt_filter;
		private System.Windows.Forms.Button button2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.ListBox lbx_list;
		private System.Windows.Forms.TextBox txt_search;
		private System.Windows.Forms.TextBox txt_content;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label2;
		MainForm mainForm;
		string dir_sent = Application.StartupPath + "\\Sent";
		
		public Mail(MainForm mainForm)
		{
			this.mainForm = mainForm;
			InitializeComponent();
			
			//Starting load all sented email
			string[] files = Directory.GetFiles( dir_sent, "*.mail" );
			foreach( string file in files )
				lbx_list.Items.Add( file.Split('\\')[file.Split('\\').Length-1] );
			
			lbx_list.SelectedIndex = 0;
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.label2 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.txt_content = new System.Windows.Forms.TextBox();
			this.txt_search = new System.Windows.Forms.TextBox();
			this.lbx_list = new System.Windows.Forms.ListBox();
			this.label1 = new System.Windows.Forms.Label();
			this.button2 = new System.Windows.Forms.Button();
			this.txt_filter = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(8, 8);
			this.label2.Name = "label2";
			this.label2.TabIndex = 4;
			this.label2.Text = "Search";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(640, 424);
			this.button1.Name = "button1";
			this.button1.TabIndex = 2;
			this.button1.Tag = "load";
			this.button1.Text = "&Load";
			this.button1.Click += new System.EventHandler(this.ButtonEvents);
			// 
			// txt_content
			// 
			this.txt_content.Location = new System.Drawing.Point(240, 8);
			this.txt_content.Multiline = true;
			this.txt_content.Name = "txt_content";
			this.txt_content.Size = new System.Drawing.Size(472, 408);
			this.txt_content.TabIndex = 1;
			this.txt_content.Text = "textBox1";
			// 
			// txt_search
			// 
			this.txt_search.Location = new System.Drawing.Point(56, 8);
			this.txt_search.Name = "txt_search";
			this.txt_search.Size = new System.Drawing.Size(176, 20);
			this.txt_search.TabIndex = 3;
			this.txt_search.Tag = "search";
			this.txt_search.Text = "";
			this.txt_search.TextChanged += new System.EventHandler(this.ButtonEvents);
			// 
			// lbx_list
			// 
			this.lbx_list.Location = new System.Drawing.Point(8, 32);
			this.lbx_list.Name = "lbx_list";
			this.lbx_list.Size = new System.Drawing.Size(224, 381);
			this.lbx_list.TabIndex = 0;
			this.lbx_list.Tag = "list";
			this.lbx_list.SelectedIndexChanged += new System.EventHandler(this.ButtonEvents);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 416);
			this.label1.Name = "label1";
			this.label1.TabIndex = 4;
			this.label1.Text = "Filter";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(552, 424);
			this.button2.Name = "button2";
			this.button2.TabIndex = 2;
			this.button2.Tag = "cancel";
			this.button2.Text = "&Cancel";
			this.button2.Click += new System.EventHandler(this.ButtonEvents);
			// 
			// txt_filter
			// 
			this.txt_filter.Location = new System.Drawing.Point(40, 416);
			this.txt_filter.Name = "txt_filter";
			this.txt_filter.Size = new System.Drawing.Size(192, 20);
			this.txt_filter.TabIndex = 3;
			this.txt_filter.Tag = "filter";
			this.txt_filter.Text = "";
			this.txt_filter.TextChanged += new System.EventHandler(this.ButtonEvents);
			// 
			// Mail
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(722, 456);
			this.Controls.Add(this.txt_search);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.txt_content);
			this.Controls.Add(this.lbx_list);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.txt_filter);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.label2);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "Mail";
			this.Text = " .:: Sent Mail List";
			this.ResumeLayout(false);
		}
		#endregion
		
		void ButtonEvents(object sender, System.EventArgs e)
		{
			switch( ((Control)sender).Tag.ToString().ToLower() ){
				case "cancel": this.Close(); break;
				case "load": 
					mainForm.loadMail( dir_sent + "\\" + lbx_list.SelectedItem.ToString() ); 
					this.Close(); break;
				case "list": viewMail( dir_sent + "\\" + lbx_list.SelectedItem.ToString() ); break;
				case "search":					
					for( int i=0; i< lbx_list.Items.Count; i++ ){
						if( lbx_list.Items[i].ToString().IndexOf(txt_search.Text) >= 0 ){
							lbx_list.SelectedIndex = i;
							return;
					}}
					break;
				case "filter":					
					lbx_list.Items.Clear();
					string[] files = Directory.GetFiles( dir_sent, "*"+txt_filter.Text+"*.mail" );
					foreach( string file in files )
						lbx_list.Items.Add( file.Split('\\')[file.Split('\\').Length-1] );
					break;
			}
		}
		
		void viewMail(string fileName){
			DataSet ds = new DataSet();
			ds.ReadXml( fileName );
			
			txt_content.Text = "";
			foreach( DataColumn col in ds.Tables[0].Columns ){
				txt_content.Text += "[" + col.ColumnName.ToUpper() + "]: " + ds.Tables[0].Rows[0][col.ColumnName] + "\r\n";
			}
		}
	}
}
